package com.company.genealogy.web.controller;

import com.company.genealogy.common.result.ApiResponse;
import com.company.genealogy.web.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 首页控制器
 * 提供首页所需的各种数据接口
 * @author chenzhixiang
 */
@RestController
@RequestMapping("/api/v1/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 获取今日提醒
     */
    @GetMapping("/reminders")
    public ApiResponse<Object> getTodayReminders() {
        try {
            return ApiResponse.success(homeService.getTodayReminders());
        } catch (Exception e) {
            return ApiResponse.fail("获取今日提醒失败: " + e.getMessage());
        }
    }

    /**
     * 获取家族统计信息
     */
    @GetMapping("/stats")
    public ApiResponse<Object> getFamilyStats(@RequestParam(value = "clanId", required = false) Long clanId) {
        try {
            return ApiResponse.success(homeService.getFamilyStats(clanId));
        } catch (Exception e) {
            return ApiResponse.fail("获取家族统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取家族动态
     */
    @GetMapping("/news")
    public ApiResponse<Object> getFamilyNews(@RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "5") int size,
                                   @RequestParam(value = "clanId", required = false) Long clanId) {
        try {
            return ApiResponse.success(homeService.getFamilyNews(page, size, clanId));
        } catch (Exception e) {
            return ApiResponse.fail("获取家族动态失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取动态点赞详情
     */
    @GetMapping("/news/likes")
    public ApiResponse<Object> getNewsLikes(@RequestParam("newsIds") List<Long> newsIds) {
        try {
            return ApiResponse.success(homeService.getNewsLikes(newsIds));
        } catch (Exception e) {
            return ApiResponse.fail("获取点赞详情失败: " + e.getMessage());
        }
    }

    /**
     * 获取热门分支
     */
    @GetMapping("/hot-branches")
    public ApiResponse<Object> getHotBranches(@RequestParam(defaultValue = "5") int limit,
                                    @RequestParam(required = false) Long clanId) {
        try {
            return ApiResponse.success(homeService.getHotBranches(limit, clanId));
        } catch (Exception e) {
            return ApiResponse.fail("获取热门分支失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户贡献统计
     */
    @GetMapping("/contributions")
    public ApiResponse<Object> getUserContributions() {
        try {
            return ApiResponse.success(homeService.getUserContributions());
        } catch (Exception e) {
            return ApiResponse.fail("获取用户贡献失败: " + e.getMessage());
        }
    }

    /**
     * 获取即将到来的活动
     */
    @GetMapping("/upcoming-events")
    public ApiResponse<Object> getUpcomingEvents(@RequestParam(defaultValue = "3") int limit) {
        try {
            return ApiResponse.success(homeService.getUpcomingEvents(limit));
        } catch (Exception e) {
            return ApiResponse.fail("获取即将到来的活动失败: " + e.getMessage());
        }
    }

    /**
     * 获取家族人口分布
     */
    @GetMapping("/population-distribution")
    public ApiResponse<Object> getPopulationDistribution() {
        try {
            return ApiResponse.success(homeService.getPopulationDistribution());
        } catch (Exception e) {
            return ApiResponse.fail("获取人口分布失败: " + e.getMessage());
        }
    }

    /**
     * 获取世代人数统计
     */
    @GetMapping("/generation-stats")
    public ApiResponse<Object> getGenerationStats() {
        try {
            return ApiResponse.success(homeService.getGenerationStats());
        } catch (Exception e) {
            return ApiResponse.fail("获取世代统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取年龄结构分析
     */
    @GetMapping("/age-structure")
    public ApiResponse<Object> getAgeStructure() {
        try {
            return ApiResponse.success(homeService.getAgeStructure());
        } catch (Exception e) {
            return ApiResponse.fail("获取年龄结构失败: " + e.getMessage());
        }
    }

    /**
     * 获取首页仪表盘数据
     */
    @GetMapping("/dashboard")
    public ApiResponse<Object> getDashboard() {
        try {
            Map<String, Object> dashboard = homeService.getDashboard();
            return ApiResponse.success(dashboard);
        } catch (Exception e) {
            return ApiResponse.fail("获取仪表盘数据失败: " + e.getMessage());
        }
    }
} 